import * as gcoord from './gcoord.js'; //引入请求地址

// 消息提醒方法的封装
function tips(type, title, conent, icon) {
	return new Promise((resolve, reject) => {
		title = title || '';
		conent = conent || '';
		if (type === 'showload') {
			// 加载弹框
			uni.showLoading({
				title: title,
				mask: true,
				success: () => {
					resolve()
				}
			});
		} else if (type === "hideload") {
			// 隐藏加载弹框
			uni.hideLoading();
		}
		if (type === 'showtoast') {
			icon = icon || 'none';
			// 加载弹框
			uni.showToast({
				title: title,
				icon: "none",
				mask: true,
				duration: 2000,
				success: () => {
					resolve()
				}
			});
		} else if (type === "hidetoast") {
			// 隐藏加载弹框
			uni.hideToast();
		} else if (type === 'showmodal') {
			uni.showModal({
				title: title,
				content: conent,
				showCancel: icon, //是否显示取消按钮
				success: (res) => {
					if (res.confirm) {
						resolve()
					}
				}
			});
		} else if (type === "hidetoast") {
			// 隐藏加载弹框
			uni.hideToast();
		}
	})
};
// 加载字体
function loadfont(typelist) {
	typelist.forEach(item => {
		if (item === 'Medium') {
			uni.loadFontFace({
				global: true,
				family: 'Medium',
				source: 'url("//at.alicdn.com/wf/webfont/F6EgKrEW9Obu/SpxViKVfYJ7o.woff")',
			})
		} else if (item === "Stride") {
			uni.loadFontFace({
				global: true,
				family: 'Stride',
				source: 'url("//at.alicdn.com/wf/webfont/F6EgKrEW9Obu/K9WwfdOGKqoB.woff")',
			})

		}
	})

}

// 页面跳转
function jump(type, url) {
	// 保留当前页面，跳转到应用内的某个页面。
	if (type === 'nav') {
		uni.navigateTo({
			url: url
		})
		// 关闭当前页面，返回上一页面或多级页面
	} else if (type === 'bck') {
		uni.navigateBack({
			delta: url
		})
		// 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
	} else if (type === 'swt') {
		if (url.indexOf('?') > -1) {
			var obj = url.split("?")[1]
			url = url.split("?")[0]
			uni.setStorageSync(obj.split("=")[0], obj.split("=")[1])
		}
		uni.switchTab({
			url: url
		})

		// 关闭当前页面，跳转到应用内的某个页面
	} else if (type === 'rdt') {
		uni.redirectTo({
			url: url
		})
		// 关闭所有页面，打开到应用内的某个页面。
	} else if (type === 'rel') {
		uni.reLaunch({
			url: url
		})
	}
}
// 对缓存的封装
function cache(type, key, conent) {
	if (!conent) {
		conent = ''
	} else {
		conent = JSON.stringify(conent)
	}
	// 存储 
	if (type === 's') {
		uni.setStorageSync(key, conent)
	} else if (type === 'g') {
		var value = uni.getStorageSync(key)
		if (value) {
			value = JSON.parse(value)
		}
		return value
	} else if (type === 'c') {
		uni.clearStorageSync()
	} else if (type === 'r') {
		uni.removeStorageSync(key)
	}
}

// 节点信息
function select(type, node, that) {
	return new Promise((resolve) => {
		if (!that) {
			that = null
		}
		// 设备信息
		if (type === 'device') {
			uni.getSystemInfo({
				success(res) {
					resolve(res)
				}
			})
		} else if (type === 'single') {
			// 单个节点
			uni.createSelectorQuery().in(that).select(node).boundingClientRect(
				res => {
					resolve(res)
				}).exec()
		}
	})
}

// 对接口传值进行加解密
function setcodeURI(type, data) {
	if (type === 'en') {
		return encodeURIComponent(JSON.stringify(data))
	} else if (type === 'de') {
		return JSON.parse(decodeURIComponent(data))
	}
}

// 对tabar的操作
function setTabBar(type) {
	if (type) {
		uni.showTabBar({
			animation: false
		})
	} else {
		// 隐藏默认的taar
		uni.hideTabBar({
			animation: false
		})
	}
}

// 扫码功能
function scanCode() {
	return new Promise((resolve) => {
		uni.scanCode({
			scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
			success: (res) => {
				resolve(res.result)
			}
		});
	})
};

// 对位置接口的封装
function location(type, obj) {
	return new Promise((resolve, reject) => {
		if (type === 'get') {
			uni.getLocation({
				type: 'wgs84' || obj.type,
				geocode: true,
				isHighAccuracy: true,
				success: (res) => {
					resolve(res)
				},
			})
		} else if (type === 'choose') {
			// 打开地图选择位置
			uni.chooseLocation({ //gcj02
				success: (res) => {
					let arr = gcoord.transform(
						[res.longitude, res.latitude], // 经纬度坐标
						gcoord.GCJ02, // 当前坐标系
						gcoord.WGS84
					)
					// uniapp的请求
					uni.request({
						url: "https://apis.map.qq.com/ws/geocoder/v1/",
						data: {
							location: res.latitude + "," + res.longitude,
							key: "F3KBZ-RN2WZ-FDTXG-TG2J5-WM6N5-DHFWJ",
						},
						method: "GET",
						success: (result) => {
							var location = result.data.result.location
							location.lng = arr[0]
							location.lat = arr[1]
							resolve(result.data.result)
						},

					});
				}
			});
		}
	})
}
// 地图城市地址转坐标
function Address2Code(address) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: 'https://apis.map.qq.com/ws/geocoder/v1/',
			data: {
				address: address,
				key: "F3KBZ-RN2WZ-FDTXG-TG2J5-WM6N5-DHFWJ",
			},
			method: "GET",
			success: (result) => {
				resolve(result.data.result)
			}
		})
	})
}

module.exports = {
	tips, //消息提醒方法的封装
	loadfont, //加载字体
	jump, //页面跳转
	cache, //对缓存的封装
	select, //节点信息
	setcodeURI, //对接口传值进行加解密
	setTabBar, //对tabar的操作
	scanCode, //扫码功能】
	location, //对位置接口的封装
	Address2Code,
}